<?php

namespace Summer\Shop\Http\Controllers;

use Illuminate\Routing\Controller;
use RLuders\JWTAuth\Classes\JWTAuth;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Input;
use Exception;
use Db;
use Summer\Shop\Models\BrowseModel;
use Summer\Shop\Models\CategoryModel;
use Summer\Shop\Models\FavoriteModel;
use Summer\Shop\Models\ProductModel;
use Summer\Shop\Models\ServiceModel;

class ProductController extends Controller
{
    use ApiReturn;
    /**
     * 商品列表
     */
    public function index(JWTAuth $auth)
    {
        $user = $auth->user();
        $return['user']=$user;
        $page=Input::get('page',1);
        $page_size=Input::get('page_size',20);
        $query=ProductModel::select();
        //筛选
        //品牌筛选
        $brand_id=Input::get('brand_id',0);
        if($brand_id>0){
            $query->where('brand_id',$brand_id);
        }
        //分类筛选
        $category_id=Input::get('category_id',0);
        if($category_id>0){
            //获取自己及子分类id
            $category=CategoryModel::where('id',$category_id)->first();
            if($category){
                $category_ids=$category->getAllChildrenAndSelf()->modelKeys();
                if($category_ids){
                    $query->whereIn('category_id',$category_ids);
                }
            }
        }
        //状态筛选-是否新品
        if(Input::has('is_new')){
            $is_new=Input::get('is_new');
            $query->where('is_new',$is_new);
        }
        //状态筛选-是否热销
        if(Input::has('is_hot')){
            $is_hot=Input::get('is_hot');
            $query->where('is_hot',$is_hot);
        }
        //状态筛选-是否推荐
        if(Input::has('is_recommend')){
            $is_recommend=Input::get('is_recommend');
            $query->where('is_recommend',$is_recommend);
        }
        //状态筛选-是否首页展示
        if(Input::has('is_home_show')){
            $is_home_show=Input::get('is_home_show');
            $query->where('is_home_show',$is_home_show);
        }
        //排序-兼容shopro
        if(Input::has('order')){
            $order=Input::get('order');
            foreach ($order as $oak=>$oav){
                if($oav>0){
                    switch ($oak)
                    {
                        case 'priceOrder':
                            if($oav==1){
                                $query->orderBy('price','asc');
                            }else{
                                $query->orderBy('price','desc');
                            }
                        break;
                        case 'salesOrder':
                            $query->orderBy('sales','desc');
                        break;
                        case 'newProdcutOrder':
                            $query->orderBy('created_at','desc');
                        break;
                        default:
                        break;
                    }
                }
            }
        }
        //排序
//        if(Input::has('sort')){
//            $sort=Input::get('sort');
//            $sort_arr=explode('|',$sort);
//            $query->orderBy($sort_arr[0],$sort_arr[1]);
//        }
        //搜索
        if(Input::has('keywords')){
            $keywords='%'.Input::get('keywords').'%';
            $query->where('name','like',$keywords);
        }
        $query->where('status',1);
        $query->with(['image_preview','images']);
        $records=$query->paginate($page_size,$page);
        $return['records']=$records;

        return $this->apiSuccess('操作成功',$return);
    }

    public function preview(JWTAuth $auth){


        if(Input::has('token')){
            $token=Input::get('token');
            $user=$auth->setToken($token)->authenticate();
        }else{
            $user=[];
        }

        $product_id=Input::get('id');
        $query=ProductModel::select();
        $query->with([
            'image_preview',
            'images'
        ]);
        $query->where('id',$product_id);
        $record=$query->first();
        //兼容uview-ui的u-swiper
        $record->images->each(function ($image){
            $image->image=$image->path;
        });
        $service_ids=$record->service_ids;
        if(!empty($service_ids)){
            $services=ServiceModel::with(['cover'])->whereIn('id',$service_ids)->get();
            $services->each(function ($service){
                $service->image=$service->cover->path;
            });
            $record->service=$services;
        }else{
            $record->service=[];
        }
        $record->favorite='';
        if($user){
            //判断是否收藏
            $record->favorite=FavoriteModel::where('user_id',$user->id)->where('product_id',$record->id)->first();
            //增加浏览足迹
            $browse=BrowseModel::where('user_id',$user->id)->where('product_id',$record->id)->first();
            if(!$browse){
                $browse_table=new BrowseModel();
                $browse_table->user_id=$user->id;
                $browse_table->product_id=$record->id;
                $browse_table->save();
            }
        }
        $return['record']=$record;
        return $this->apiSuccess('操作成功',$return);
    }

}
